using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace XYTerrain.Editor 
{


    ///  用于准备空的层数据用于区域划分
    public class PerpareEnmptyLayer : EditorWindow
    {

        public List<LayerData> layerDatas = new List<LayerData>();
        List<LayerData> removeOnEnd = new List<LayerData>();
        public Terrain curreTerrain;

        public int splitTime = 1;
        int _splitTime = 0;








        [MenuItem("Tools/Nature Manufacture/XY/LayerCreater")]
        static void Init(){

            PerpareEnmptyLayer window = EditorWindow.GetWindow<PerpareEnmptyLayer>("Layer Creater");
            window.Show();



        }

         void OnGUI(){


            splitTime = (EditorGUILayout.IntSlider("分割次数", splitTime, 2, 32));

            
            
            DrawLayerList();
            EditorGUILayout.Space(5);
            DrawCreat();
            ActOnDrawEnd();
         }


        void DrawCreat()
        {

            if (GUILayout.Button("生成"))
            {
                GenerateEmptyLayer();
            }
            if(Selection.activeGameObject==null){
                EditorGUILayout.HelpBox("先选中地形文件后点击生成", MessageType.Warning);
            }
        }
        void DrawLayerList()
        {

            EditorGUILayout.BeginVertical(EditorStyles.helpBox);
            EditorGUILayout.LabelField("预定义层级列表");
            foreach (var i in layerDatas)
            {

                EditorGUILayout.Space(1);

                EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
                i.LayerName = EditorGUILayout.TextField("Layer名称:", i.LayerName);
                RemoveBtn(i);
                EditorGUILayout.EndVertical();
                EditorGUILayout.Space(1);

            }
            if (GUILayout.Button("Add"))
            {
                layerDatas.Add(new LayerData());
            }
            EditorGUILayout.EndVertical();
        }

        private bool RemoveBtn(LayerData data)
        {
            bool down = GUILayout.Button("X");
            if (down)
            {
                removeOnEnd.Add(data);
            }
            return down;
        }

        private void ActOnDrawEnd()
        {
            foreach(var i in removeOnEnd)
            {
                    layerDatas.Remove(i);
            }
            removeOnEnd.Clear();
        }


        


        public void GenerateEmptyLayer()
        {
            if (Selection.gameObjects == null) return;
            curreTerrain = Selection.activeGameObject.GetComponent<Terrain>();
            if (curreTerrain == null) return;
            var size = curreTerrain.terrainData.size;

            var ulitSize = size/splitTime;
            ulitSize.y = size.y;

            Vector3[] splitPoinList = new Vector3[splitTime * splitTime];
            Vector3 centerPoin = curreTerrain.transform.position;

            centerPoin.x += 0.5f * ulitSize.x;
            centerPoin.z += 0.5f * ulitSize.z;

            for (int x = 0; x < splitTime; x++)
            {
                for (int z = 0; z < splitTime; z++)
                {
                    splitPoinList[x * splitTime + z] = centerPoin + new Vector3(x * ulitSize.x, 0, z * ulitSize.z);
                }

            }



            GameObject o = new GameObject("cacheObject");
            o.transform.position = curreTerrain.transform.position;

            for (int i = 0; i < splitPoinList.Length; i++)
            {
                var pos = splitPoinList[i];
                // TODO  更变命名方式
                var p = new GameObject($"X{i / splitTime}_Z{i % splitTime}");
                p.transform .position = pos;
                p.transform.parent = o.transform;
            }

            foreach (var i in layerDatas)
            {
                var layer = GameObject.Instantiate(o);
                layer.name = i.LayerName;
                var parent = layer.AddComponent<XYTerrain.Runtime.ObjectsParent>();
                parent.gameObjectPrefix = i.LayerName;



            }

            DestroyImmediate(o);




        }





    }




    public class LayerData
    {
        public string LayerName;

    }
}